// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <benchmark/benchmark.h>

#include <vector>

#include <nebula/compute/api_scalar.h>
#include <tests/compute/kernels/test_util.h>
#include <nebula/testing/ktest_util.h>
#include <nebula/testing/random.h>
#include <nebula/util/benchmark_util.h>

namespace nebula {
namespace compute {

constexpr auto kSeed = 0x94378165;

using BooleanBinaryOp = turbo::Result<Datum>(const Datum&, const Datum&, ExecContext*);

template <BooleanBinaryOp& Op>
static void ArrayArrayKernel(benchmark::State& state) {
  RegressionArgs args(state);

  const int64_t array_size = args.size * 8;

  auto rand = random::RandomArrayGenerator(kSeed);
  auto lhs = rand.Boolean(array_size, /*true_probability=*/0.5, args.null_proportion);
  auto rhs = rand.Boolean(array_size, /*true_probability=*/0.5, args.null_proportion);

  for (auto _ : state) {
    ABORT_NOT_OK(Op(lhs, rhs, nullptr).status());
  }
  state.SetItemsProcessed(state.iterations() * array_size);
}

void SetArgs(benchmark::internal::Benchmark* bench) {
  BenchmarkSetArgsWithSizes(bench, {kL1Size, kL2Size});
}

BENCHMARK_TEMPLATE(ArrayArrayKernel, And)->Apply(SetArgs);
BENCHMARK_TEMPLATE(ArrayArrayKernel, KleeneAnd)->Apply(SetArgs);

}  // namespace compute
}  // namespace nebula
